<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="zhaozhenguo">
		<title>报废单</title>
		<!--js库及样式-->
		<script LANGUAGE="javascript" src="../script/common.js" type="text/javascript"></script>
		<!--动态js--> 
		<script language="javascript" type="text/javascript">
			//全局变量
			var org = Common.cOrg();	//当前单位
			var oper=Common.cOperator();
			var qt = {};				//数据表格对象
			var qtmx = {};
			var orderid;//GiftOrder
		    var giftid;
			//页面加载方法
			function load(){
				Eui.showNavigation();	
				setPages(["listview","itemlists","insertview","updateview","newinventoryorder","printview"]);
				createTable();	
			}
			function createTable(){
				var columns = [];
				columns[0]={type:"checkbox"}
				columns[5]={title:"操作",width:"20%",render:showItems,order:false}
				/*var columns = [	
					{type:"checkbox"},
					{title:"礼品报废单号",width:"20%"},
					{title:"参考外部单据号",width:"20%"},
					{title:"礼品管理员",width:"20%"},
					{title:"礼品报废时间",width:"20%",render:function(v){return v?v.format():""}},
					{title:"操作",width:"20%",render:showItems,order:false}
				]*/
				qt = new QueryTable("gift_bill_reject_query_bill","listform","datalist",columns,{
					buttons:[
						{value:"增 加",actions:onNewOrder},
						{value:"删 除",actions:onDeleteOrder}
					]
				});
			}
			function itemCreateTable(){
				var columns =[];
				columns[0]={type:"checkbox"}
				columns[7]={title:"操作",width:"15%",render:updateItem,order:false}
				/*var columns = [
					{type:"checkbox"},		//复选框
					{title:"商品大类型",width:"15%"},	
					{title:"商品子类型",width:"15%"},	                      //点击进入修改
					{title:"商品名称",width:"15%"},						
					{title:"报废数量",width:"14%"},					
					{title:"计量单位",width:"10%"},
					{title:"备注",width:"16%"},
					{title:"操作",width:"15%",render:updateItem,order:false}
				]*/
				qtmx = new QueryTable("gift_bill_reject_query_item","","itemlist",columns,{showData:"true",
					buttons:[
						{value:"增 加",actions:onNewItem},
						{value:"删 除",actions:onDeleteItems},
						{value:"返 回",actions:resetFunc}
					]
				});
				qtmx.paramFunc = function(){
					var params = {};
					params.giftOrder = orderid;
					return params;
				}
			}
			function showData(){
				qt.showTable(); 
			}
			function showItems(v,r){
				return "<a href=# onclick=\"showItems2("+r[0]+",'"+r[1]+"','"+r[2]+"','"+r[3]+"','"+r[4]+"');return false;\">操作</a>"
			}
			function updateItem(v,r){
				return "<a href=# onclick=\"updateItem2("+r[0]+",'"+r[3]+"','"+r[4]+"','"+r[5]+"','"+r[6]+"');return false;\">修改</a>"
			}
			//进入明细页面
		    function updateItem2(id,giftName,reqQty,baseUOM,comments){
				if (id != null){
					$("updateform").id.value=id;
			        $("giftName2").innerHTML=giftName;
			        $("updateform").quantity.value=reqQty;
			        $("baseUOM2").innerHTML=baseUOM;
			        if(comments=="null"){
			        	comments="";
			        }
			        $("updateform").comments.value=comments;
				    switchPage("updateview");
				}
			}
			var codeids;
			var codeids2;
			var dates;
			function showItems2(id,codeid,codeid2,oper,date){
				orderid=id;
			    var datetime = new Date(date)
			 	if (id != null){
			 		$("orderCode").innerHTML=codeid;
			 		$("refCode").innerHTML=codeid2;
	               	$("operator").innerHTML=oper;
	               	$("checkDateTime").innerHTML=date.toString();
			        itemCreateTable();
					switchPage("itemlists");
				}
			}
			//显示新增界面
			function update2(){
				if(!checkForm(updateform))return;
			    var id=updateform.id.value;
			    var qu=updateform.quantity.value;
			    var comments=updateform.comments.value;
				DWR.call('giftStorageService.updateOutInventoryItem',id,qu,comments,function(v){
				    itemCreateTable(); 
					switchPage('itemlists');
				    if(v){
				    	showError("你输入的数量不能大于库存数量");
				    }
			    });
			}
			function onNewOrder(){
				switchPage("newinventoryorder");
			}
			//选择单位控件
			function selOrg(){			
				var v = openDialogFrame("/client/loy_giftinventory_list.html",'选择商品',500,600)
				if (v){		
	            	insertform.giftName.value = v.name;
				    giftid=v.id;
				}
			}
			function selOrg2(){			
				var v = openDialogFrame("/client/loy_giftinventory_list.html",'选择商品',500,600)
				if (v){		
	                $("caigoumxxz").giftName.value = v.name;
				    giftid=v.id;
				}
			}
			//取消方法
			function resetFunc(){
				switchPage('listview');
				showData();
			}
			//保存方法
			function onSaveOrAddOrder(){
			    if(!checkForm("inventoryorder"))return;
		        var inventoryorder1 = Common.getValues("inventoryorder");
			    if (!inventoryorder1.id)
		        inventoryorder1.operator=oper.id;
		        inventoryorder1.flag=1;
		        inventoryorder1.giftLocation=org.id;  
				DWR.call('giftStorageService.createInventoryOrder',inventoryorder1,Constants.GIFT_INVENTORY_reject,function(v){//礼品报废为4
			        switchPage('insertview');
			        getOrderId(v.id,v.orderCode,v.refCode,v.checkDateTime);	          
			    });
			    function getOrderId(id,orderCode,refCode,checkDateTime){
				    var datetime=new Date(checkDateTime);
				    orderid=id;
				    codeids=orderCode;
		            codeids2=refCode;
		            dates=datetime.format();     
			    }		 
			}  
			function onSaveOrAddItem(){
			   if(!checkForm(insertform))return;
		        var inventoryitem = Common.getValues(insertform);
			    if (!inventoryitem.id)
			    inventoryitem.inventoryOrder=orderid;
			    inventoryitem.gift=giftid;  
				DWR.call('giftStorageService.addOutInventoryItem',inventoryitem,function(v){
					if(v==1){
			        	showError("添加失败，已有此商品了");
			        }else if(v==2){
			        	showError("库存不足");
			        }else{
			        	showInformation("添加成功!"); 
					}  
		    	});
			    insertform.reset();
			}
			function onNewItem(){
				var inventoryitem = Common.getValues(insertform);
				DWR.call('giftStorageService.addOutInventoryItem',inventoryitem,function(v){
					switchPage('insertview');
				});	
			}
			function qx(){
				switchPage('selectdetview');
			}
			function xzFunc(){
				$("caigoumxxz").reset();
				switchPage('detviewmxxz');
			}
			function resetItem(){
		    	insertform.reset();
			}
			function resetItem2(){
		    	insertform.reset();
			}
			//删除方法
			function onDeleteItems(){
				var	id = qtmx.getBoxValue();
				if(id==null||id==""){
					showWarning("请选择您确定要删除的数据");return;
				}
				if(showQuestion("确定删除吗?")==false){
					return;
				}
				var ids = id.split(",");
				DWR.call('giftStorageService.deleteOutInventoryItemByIds',ids,function(v){
			    	itemCreateTable(); 
			        showInformation("删除成功!");
			    });		
				switchPage('itemlists');
			}
			function onDeleteOrder(){  
				var	id = qt.getBoxValue();
				if(id==null||id==""){
			    	showWarning("请选择您确定要删除的数据");return;
				}
				if(showQuestion("确定删除吗?")==false){
					return;
				}
				var ids = id.split(",");		
				DWR.call('giftStorageService.deleteOutInventoryOrderByIds',ids,function(v){
				    showData();
				    showInformation("删除成功!");
			    });
				switchPage('listview');
			}
	        function onBackItemList(){ 
		        itemCreateTable();
				if(codeids!=null){
					$("orderCode").innerHTML=codeids;
					$("refCode").innerHTML=codeids2;
			        $("operator").innerHTML=oper.name;
			        $("checkDateTime").innerHTML=dates;
				}    
		        switchPage('itemlists');
	        }
			function onCancl1(){
				switchPage('listview');
			}
			function onCancl2(){
				switchPage('itemlists');
			}
			function back(){
				createTable();
				showData();
				switchPage('listview');			
			}
			function save(){
			  	DWR.call('giftStorageService.toHos',orderid,function(v){  
			  	giftId =v.id;
			  	requestframe.document.location.href = "print_reject_gift.html";
			  	switchPage("printview"); 
			});
			}	
		</script>
	</HEAD>
	<body onload="load()">
		<!--页面导航-->
		<div class="nav"><div id="nav">&nbsp;</div></div>
		<div id="listview">	
			<div class="searchForm">
				<form id="listform" name="listform">
					<table cellSpacing=0 cellPadding=4 width="100%" border=0>
					    <tr>
						    <td><label>报废单号</label> <input type="text" name="instoCode" maxlength=15></td>
						    <td><label>报废日期</label> <input type="text" name="instoDate" onblur="checkV(this,'date')"><a href="javascript:void(0)" onclick='showDatePicker( $("instoDate"));' class="date" title="选择日期">&nbsp;&nbsp;</a></td>
							<td><button type="button" onclick="showData()">查 询</button></td>
						</tr>
					</table>
				</form>
			</div>	
			<div id="datalist"></div>	
		</div>
		
		<div id="itemlists" style="display:none;">
			<div class="form">
				<form id="listform1" name="listform1">
					<input type="hidden" name="giftOrder">
					<input type="hidden" name="organization">	
					<div class="formTitle">礼品报废单详细信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable">
						<tr>
							<td><label>报废单号</label> <span id="orderCode"></span></td>
							<td><label>参考外部单据号</label> <span id="refCode"></span></td>
						</tr>
						<tr>		
							<td><label>报废日期</label> <span id="checkDateTime"></span></td>
						    <td><label>操&nbsp;&nbsp;&nbsp;作&nbsp;&nbsp;&nbsp;员&nbsp;&nbsp;</label> <span id="operator"></span></td>
						</tr>
					</table>
				</form>	 
			</div>
			<div id="itemlist"></div>	
			<p>
				<button type="button" id="binSend" onclick="save()">保 存</button>
			</p>
		</div>
		<div id="selectdetview" style="display:none;" align="center"></div>
		<div class="printview" id="printview" style='display:none;'>
			<iframe id="requestframe" name="requestframe" style="width:100%;height:90%;overflow:auto;" frameborder=0 src=""></iframe>
		</div>	
		<!--明细(新增)静态页面部分-->
		<div id="insertview" style="display:none;">
			<div class="form">
				<form id="insertform" name="insertform">
					<input type="hidden" name="id">
					<input type="hidden" name="organization">
					<div class="formTitle">商品详细信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0>
						<tr>
							<td><label>商品名称</label> <input type="text" name="giftName" onblur="checkV(this,'',true)"readonly class="readonly"><a class=orga href=# onclick="selOrg()" return false;><font class="des">*</font></td> 
							<td><label>报废数量</label> <input type=text name="quantity" onblur="checkV(this,'number',true)"  maxlength=8><font class="des">*</font></td>
						</tr>
						<tr>		
							<td><label>备&nbsp;&nbsp;注&nbsp;&nbsp;</label> <input type="text" name="comments"  maxlength="32"></td>
						</tr>		
					</table>
					<div align="center"><button type="button" onclick="onSaveOrAddItem()">添 加</button> <button type="button" onclick="resetItem()">清 除</button> <button type="button" onclick="onBackItemList()">返 回</button></div>
				</form>
			</div>
		</div>
		
		<div id="updateview" style="display:none;">
			<div class="form">
				<form id="updateform" name="updateform">
					<input type="hidden" name="id">
					<input type="hidden" name="organization">
					<div class="formTitle">修改商品信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="viewTable label6">
						<tr>
							<td><label>商品名称</label> <span id="giftName2"></span></td>
							<td><label>报废数量</label> <input type="text" name="quantity" onblur="checkV(this,'number',true)" maxlength=15><font class="des">*</font></td>
						</tr>
						<tr>		
							<td><label>计量单位</label> <span id="baseUOM2"/></td>
							<td><label>备&nbsp;&nbsp;注</label> <input type="text" name="comments"  maxlength="32"></td>
						</tr>			
					</table>
					<div align="center"><button type="button" onclick="update2()">保 存</button> <button type="button" onclick="onCancl2()">取 消</button></div>
				</form>
			</div>
		</div>
		
		<div id="newinventoryorder" style="display:none;">
			<div class="form">
				<form id="inventoryorder" name="inventoryorder">
					<div class="formTitle">添加礼品报废单据</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0>
						<tr>
							<td><label>参考外部单据号</label> <input type="text" name="refCode" onblur="checkV(this,'',true)"  maxlength=15><font class="des">*</font></td>
						</tr>					
					</table>
					<div align="center"><button type="button" onclick="onSaveOrAddOrder()">保 存</button> <button type="button" onclick="onCancl1()">取 消</button></div>
				</form>
			</div>
		</div>
	</body>
</HTML>